package com.zhouzi.zhaoyang.adapter.rvadapter.common;

import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private SparseArray<View> views;
    private Context mContext;
    private View convertView;

    private BaseAdapter.OnItemClickListener mOnItemClickListener ;

    public BaseViewHolder(View itemView, BaseAdapter.OnItemClickListener onItemClickListener){
        super(itemView);
        convertView=itemView;
        itemView.setOnClickListener(this);
        mContext=itemView.getContext();
        this.mOnItemClickListener = onItemClickListener;
        this.views = new SparseArray<View>();
    }


    public View getConvertView(){
       return convertView;
    }

    public TextView getTextView(int viewId) {
        return retrieveView(viewId);
    }
    public Button getButton(int viewId) {
        return retrieveView(viewId);
    }
    public ImageView getImageView(int viewId) {
        return retrieveView(viewId);
    }
    public LinearLayout getLinearLayout(int viewId) {

        return retrieveView(viewId);
    }
    public RecyclerView getRecyclerView(int viewId) {

        return retrieveView(viewId);
    }
    public View getView(int viewId) {
        return retrieveView(viewId);
    }

    protected <T extends View> T retrieveView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }



    public BaseViewHolder setImageByUrl(int viewId, String url) {
        if (!url.contains("http://")){
//            url= Url.img + url;
        }
        Log.d("setImageByUrl","url>"+url);

        return this;
    }
    public BaseViewHolder setBase64Image(int viewId, byte[] b) {
//        if (!url.contains("http://")){
//            url=Url.BASE_L + url;
//        }

        return this;
    }
    public BaseViewHolder setImageByUrl(int viewId, String url, boolean load0) {
        if (!url.contains("http://")){
        }
        Log.d("setImageByUrl","url>"+url);

        return this;
    }
    public BaseViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = (ImageView) getView(viewId);
        view.setImageResource(drawableId);
        return this;
    }
    public BaseViewHolder setText(int viewId, String text) {
        TextView view = (TextView) getView(viewId);
        view.setText(text);
        return this;
    }




    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v,getLayoutPosition());
        }
    }
}
